package com.sgb.kjwl.view.widget;

import android.content.Context;
import android.graphics.Rect;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.databinding.library.baseAdapters.BR;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;


import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ItemPopServiceTypeBinding;
import com.sgb.kjwl.model.entity.TypeEntity;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;
import com.lxj.xpopup.core.BottomPopupView;
import com.swgk.core.util.ScreenUtil;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ServiceTypePopWindow extends BottomPopupView {

    private OKCallback okCallback;
    private List<TypeEntity> mData;
    private List<TypeEntity> mChooseList = new ArrayList<>();

    public ServiceTypePopWindow(@NonNull Context context, List<TypeEntity> data, TypeEntity typeEntity, OKCallback okCallback) {
        super(context);
        mData = data;
        mChooseList.clear();
        this.okCallback = okCallback;
        if (typeEntity != null) {
            String[] split = typeEntity.getCode().split(",");
            List<String> strings = Arrays.asList(split);
            for (int i = 0; i < mData.size(); i++) {
                if (strings.indexOf(mData.get(i).getCode()) > -1) {
                    mChooseList.add(mData.get(i));
                }
            }
        }
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.popwindow_service_type;
    }

    @Override
    protected void onCreate() {
        super.onCreate();
        TextView resetView = findViewById(R.id.reset_view);
        TextView okView = findViewById(R.id.ok_view);
        TextView titleView = findViewById(R.id.title);
        titleView.setText("选择经营类型");
        RecyclerView mRecyclerView = findViewById(R.id.skill_list);

        FlowLayoutAdapter mAdapter = new FlowLayoutAdapter(getContext(), mData);
        mRecyclerView.setLayoutManager(new GridLayoutManager(getContext(), 2, GridLayoutManager.VERTICAL, false));
        mRecyclerView.addItemDecoration(new MyItemDecoration());
        mRecyclerView.setAdapter(mAdapter);

        mAdapter.setOnItemClick(new AdapterOnItemClick<TypeEntity>() {
            @Override
            public void onItemClick(TypeEntity typeEntity, int position) {
                int index = mChooseList.indexOf(typeEntity);
                if (index > -1) {
                    mChooseList.remove(index);
                } else {
                    mChooseList.add(typeEntity);
                }
                mAdapter.notifyDataSetChanged();
            }
        });

        resetView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

        okView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (okCallback != null) {
                    okCallback.onResult(mChooseList);
                }
                dismiss();
            }
        });
    }

    public interface OKCallback {
        void onResult(List<TypeEntity> list);
    }

    public class FlowLayoutAdapter extends BaseRecycleAdapter<TypeEntity> {

        public FlowLayoutAdapter(Context context, List<TypeEntity> datas) {
            super(context, R.layout.item_pop_service_type, datas);
        }

        @Override
        public void convert(ItemViewHolder holder, final TypeEntity entity) {
            holder.setBinding(BR.viewModel, entity.getName());
            int index = mChooseList.indexOf(entity);
            ItemPopServiceTypeBinding binding = (ItemPopServiceTypeBinding) holder.getmBinding();
            binding.checkBox.setBackgroundResource(index > -1 ? R.mipmap.btn_checed : R.mipmap.btn_uncheced);
        }

        @Override
        public void onAttachedToRecyclerView(@NonNull RecyclerView recyclerView) {
            super.onAttachedToRecyclerView(recyclerView);
            RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
            if (layoutManager instanceof GridLayoutManager) {
                GridLayoutManager manager = (GridLayoutManager) layoutManager;
                manager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
                    @Override
                    public int getSpanSize(int position) {
                        String name = getDatas().get(position).getName();
                        if (!TextUtils.isEmpty(name) && name.length() >= 10) {
                            return 2;
                        }
                        return 1;
                    }
                });
            }
        }
    }

    public class MyItemDecoration extends RecyclerView.ItemDecoration {

        private int mSpaceSize;
        private boolean isBottom;

        public MyItemDecoration() {
            mSpaceSize = 10;
        }

        public MyItemDecoration(boolean bottom) {
            mSpaceSize = 10;
            this.isBottom = bottom;
        }

        public MyItemDecoration(int spaceSize) {
            mSpaceSize = spaceSize;
        }

        @Override
        public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {

            int position = parent.getChildLayoutPosition(view);
            if (isBottom) {
                if (position == 0) {
                    outRect.set(0, 0, 0, 0);
                } else {
                    outRect.set(0, 0, 0, ScreenUtil.dip2px(mSpaceSize));
                }
            } else {
                outRect.set(0, 0, ScreenUtil.dip2px(mSpaceSize), ScreenUtil.dip2px(mSpaceSize));
            }
        }
    }

}
